package transaction.lcn.common.exception;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import transaction.lcn.common.entity.Result;
import transaction.lcn.common.entity.StatusCode;

/**
 * 统一异常捕获类
 *
 * @author Administrator
 **/
@ControllerAdvice//控制器增强
public class ExceptionCatch {

    private static final Logger LOGGER = LoggerFactory.getLogger(ExceptionCatch.class);

    //捕获CustomException此类异常
    @ExceptionHandler(CustomException.class)
    @ResponseBody
    public Result customException(CustomException customException) {
        customException.printStackTrace();
        //记录日志
        LOGGER.error("catch exception:{}", customException.getMessage());
        int resultCode = customException.getResultCode();
        return new Result(false, resultCode, customException.getMessage());
    }

    //捕获Exception此类异常
    @ExceptionHandler(Exception.class)
    @ResponseBody
    public Result exception(Exception exception) {
        exception.printStackTrace();
        //记录日志
        LOGGER.error("catch exception:{}", exception.getMessage());
        return new Result(false, StatusCode.ERROR, exception.getMessage());
    }

}
